package com.redrose.audioandvideo.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.Scroller;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.redrose.audioandvideo.utils.LogUtils;
import com.redrose.audioandvideo.utils.ToolUtils;

public class RrScrollLayout extends FrameLayout {
    public static final String TAG = "RrScrollLayout";
    private Context mContext;
    private Scroller mScroller;
    //上一次按下的Y坐标
    private float mLastDownY;
    //上一次抬起的Y坐标
    private float mLastUpY;
    //上一次的
    private int mLastScrollY;
    //最大高度
    int maxHeight;
    //半模高度
    int midHeight;
    //最小高度
    int minHeight;
    private int viewHeight;

    public RrScrollLayout(@NonNull Context context) {
        this(context, null);
    }

    public RrScrollLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RrScrollLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        initDate();
    }

    private void initDate() {
        mScroller = new Scroller(mContext);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        viewHeight = getHeight();
        maxHeight = viewHeight;
        minHeight = viewHeight / 4;
        midHeight = viewHeight / 2;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int action = event.getAction();
        float y = event.getY();
        switch (action) {
            case MotionEvent.ACTION_MOVE:
                move(y);
                break;
            case MotionEvent.ACTION_UP:
                mLastUpY = y;
                break;
            case MotionEvent.ACTION_DOWN:
                mLastDownY = y;
            default:
                break;
        }
        return true;
    }


    /**
     * 跟随手指滑动
     *
     * @param y
     */
    private void move(float y) {
        //滑动得距离
        float destance = y - mLastDownY;
        float deltaY;
        //计算剩余应滑动距离
        deltaY = -(destance - mLastScrollY);
        //不允许滑动出边界
        if (deltaY > 0) {
            deltaY = 0;
        }
        //最大向下滑动距离
        if (deltaY < -viewHeight) {
            deltaY = -viewHeight;
        }
        moveTo((int) deltaY);
    }

    @Override
    public void computeScroll() {
        super.computeScroll();
        if (mScroller.computeScrollOffset()) {
            scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
            postInvalidate();
        }
    }

    /**
     * 滑动到指定Y位置
     *
     * @param destY 目标坐标
     */
    private void smoothScrollTo(int destY) {
        int scrollY = getScrollY();
        int deltaY = destY - scrollY;
        mScroller.startScroll(0, scrollY, 0, deltaY, 1000);
        invalidate();
    }

    /**
     * 滑动到指定Y位置
     *
     * @param destY 剩余应滑动scrollY值
     */
    private void moveTo(int destY) {
        int scrollY = getScrollY();
        int deltaY = destY - scrollY;
        mScroller.startScroll(0, scrollY, 0, deltaY, 0);
        invalidate();
    }

    public void openMax() {
        int scrollY = getScrollY();
        Log.d(TAG, "scrollY: " + scrollY);
        smoothScrollTo(0);
    }

    public void openMid() {
        int scrollY = getScrollY();
        Log.d(TAG, "scrollY: " + scrollY);
        smoothScrollTo(-(maxHeight - midHeight));
    }

    public void openMin() {
        int scrollY = getScrollY();
        Log.d(TAG, "scrollY: " + scrollY);
        smoothScrollTo(-(maxHeight - minHeight));
    }
}
